// pages/myself/merchant/ModifyTenant.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    tenantInfo: null,
    showModalStatus: false,
    showTime: '获取验证码',
    second: 60,
    phoneSize: 0,
    mobile: '',
    phone: '',
    verifyCode1: '1',
    verifyCode2: '2'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      tenantInfo: JSON.parse(options.tenantInfo),
      phone: JSON.parse(options.tenantInfo).phone,
      showPhone: JSON.parse(options.tenantInfo).phone.substr(0, 3) + "****" + JSON.parse(options.tenantInfo).phone.substr(7)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      showModalStatus: false
    })
    utils.clearCountdown(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  // 关闭弹屏
  powerDrawer: function () {
    this.setData({
      showModalStatus: false
    });
    utils.clearCountdown(this);
  },


  /**
   * 弹出手机号修改框
   */
  topPhone: function (e) {
    this.setData({
      showModalStatus: true
    })
  },

  /**
   * 获取手号机
   */
  phoneBindinput: function (e) {
    this.setData({
      phoneSize: e.detail.cursor,
      mobile: e.detail.value
    })
  },

  /**
   * 验证码
   */
  verifyCodeBindinput: function (e) {
    this.setData({
      verifyCode1: e.detail.value
    })
  },

  /**
   * 获取验证码
   */
  getCode: function (e) {
    if (this.data.phoneSize == 11) {
      network._post_json(this, api.authCode, { mobile: this.data.mobile }, authCodeSuccess);
    } else {
      this.showZanTopTips(this.prompt, '请输入正确的手机号');
    }
  },

  /**
   * 确认手机号
   */
  modifyPhone: function (e) {
    if (this.data.verifyCode1 == this.data.verifyCode2) {
      this.setData({
        showModalStatus: false,
        phone: this.data.mobile,
        showPhone: this.data.mobile.substr(0, 3) + "****" + this.data.mobile.substr(7)
      })
      utils.clearCountdown(this);
    } else {
      this.showZanTopTips(this.prompt, '请输入正确的验证码');
    }
  },

  /**
   * 入进支付
   */
  goPayEnter: function (e) {
    console.log(e);
    let formId = e.detail.formId; // 可选
    let name = e.detail.value.name;
    let contacts = e.detail.value.contacts;
    // let phone = e.detail.value.phone;
    let code = e.detail.value.code; // 可选
    if (name == '') {
      this.showZanTopTips(this.prompt, '请输入商家名称');
      return;
    }
    if (contacts == '') {
      this.showZanTopTips(this.prompt, '请输入联系人姓名');
      return;
    }
    if (this.data.phone == '') {
      this.showZanTopTips(this.prompt, '请输入联系人手机号');
      return;
    }
    network._post_json(this, api.modifyTenant, { formId: formId, tenantId: this.data.tenantInfo.id, name: name, contacts: contacts, phone: this.data.phone, code: code }, success);
  }
}))

// 商户修改成功
var success = function (that, res) {
  if (res.data.data) {
    WxNotificationCenter.postNotificationName("RefreshMerchantBrand");
    wx.showToast({
      title: '',
    });
    setTimeout(function () {
      wx.navigateBack();
    }, 1000)

    // wx.navigateTo({
    //   url: '/pages/myself/merchant/MerchantBrand',
    // })
  }
}

var authCodeSuccess = function (that, res) {
  if (res.data.data) {
    utils.countdown(that);
    that.setData({
      verifyCode2: res.data.data.smsCode
    })
  }
}